﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class SanPhamKhuyenMaiDangBanTrenGianHangDAO
    {
        #region 1   -   Queries

        public static SanPhamKhuyenMaiDangBanTrenGianHangDTO LayRaSanPhamKhuyenMaiTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG temp = myDB.SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANGs.Single(c => c.ID == id);
            return ChuyenTuSanPhamKMDBTGHSangDTO(temp);
        }

        public static SanPhamKhuyenMaiDangBanTrenGianHangDTO LayRaSanPhamKhuyenMaiTheoMaSanPham(int masanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG temp = myDB.SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANGs.Single(c => c.MaSanPham == masanpham);
            return ChuyenTuSanPhamKMDBTGHSangDTO(temp);
        }

        public static List<SanPhamKhuyenMaiDangBanTrenGianHangDTO> LayDanhSachSanPhamKhuyenMaiTheoMaGianHang(int magianhang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListSPKM = from sp in myDB.SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANGs
                           where sp.MaGianHang == magianhang
                           select sp;

            List<SanPhamKhuyenMaiDangBanTrenGianHangDTO> result = new List<SanPhamKhuyenMaiDangBanTrenGianHangDTO>();
            foreach (SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG item in ListSPKM)
            {
                SanPhamKhuyenMaiDangBanTrenGianHangDTO temp = ChuyenTuSanPhamKMDBTGHSangDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static bool ThemSanPhamKhuyenMai(SanPhamKhuyenMaiDangBanTrenGianHangDTO sanphamkhuyenmaiDTO)
        {
            SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG temp = ChuyenTuDTOSangSanPhamKMDBTGH(sanphamkhuyenmaiDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANGs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaSanPhamKhuyenMaiTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG temp = myDB.SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANGs.Single(c => c.ID == id);
            try
            {
                myDB.SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANGs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion

        #region 2   -   Fucntion Helper

        public static SanPhamKhuyenMaiDangBanTrenGianHangDTO ChuyenTuSanPhamKMDBTGHSangDTO(SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG sanpham)
        {
            SanPhamKhuyenMaiDangBanTrenGianHangDTO result = new SanPhamKhuyenMaiDangBanTrenGianHangDTO();
            result.CacThongTinThem = sanpham.CacThongTinThem;
            result.GiaDangBan = sanpham.GiaDangBan.Value;
            result.GiaKhuyenMai = sanpham.GiaKhuyenMai.Value;
            result.ID = sanpham.ID;
            result.MaGianHang = sanpham.MaGianHang;
            result.MaSanPham = sanpham.MaSanPham;
            result.ThoiGianHetKhuyenMai = sanpham.ThoiGianHetKhuyenMai.Value;
            result.TinhTrangSanPham = sanpham.TinhTrangSanPham.Value;
            return result;
        }

        public static SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG ChuyenTuDTOSangSanPhamKMDBTGH(SanPhamKhuyenMaiDangBanTrenGianHangDTO sanphamDTO)
        {
            SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG result = new SAN_PHAM_KHUYEN_MAI_DANG_BAN_TREN_GIAN_HANG();
            result.CacThongTinThem = sanphamDTO.CacThongTinThem;
            result.GiaDangBan = sanphamDTO.GiaDangBan;
            result.GiaKhuyenMai = sanphamDTO.GiaKhuyenMai;
            result.ID = sanphamDTO.ID;
            result.MaGianHang = sanphamDTO.MaGianHang;
            result.MaSanPham = sanphamDTO.MaSanPham;
            result.ThoiGianHetKhuyenMai = sanphamDTO.ThoiGianHetKhuyenMai;
            result.TinhTrangSanPham = sanphamDTO.TinhTrangSanPham;
            return result;
        }

        #endregion
    }
}
